//
//  MineInfoNewOneCollectionViewCell.m
//  MJApp
//
//  Created by wangliang on 15/7/22.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "MineInfoNewOneCollectionViewCell.h"
#import "MineInfoNewOneCellCollectionViewCell.h"


@interface MineInfoNewOneCollectionViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong)UIPageControl *pageControl;
@property(nonatomic,strong)UICollectionView *collectionView;

@end

@implementation MineInfoNewOneCollectionViewCell


- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self UI];
    }
    return self;
}


- (void)UI {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = self.frame.size;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout];
    collectionView.pagingEnabled = YES;
    collectionView.showsHorizontalScrollIndicator = NO;
    [self addSubview:collectionView];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [collectionView registerNib:[UINib nibWithNibName:@"MineInfoNewOneCellCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"MineInfoNewOneCellCollectionViewCell"];
    collectionView.backgroundColor = Color_Back_Gray;
    self.collectionView = collectionView;
    
    CGRect pageFrame = self.frame;
    pageFrame.size.height = 50;
    pageFrame.origin.y =  self.frame.size.height - pageFrame.size.height;
    
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:pageFrame];
    pageControl.numberOfPages = 0;
    pageControl.pageIndicatorTintColor = [UIColor grayColor];
    pageControl.userInteractionEnabled = NO;
    [self addSubview:pageControl];
    self.pageControl = pageControl;
    
    self.layer.cornerRadius = 5;
    self.layer.borderColor = [UIColor grayColor].CGColor;
    self.layer.borderWidth = 0.5;
    self.clipsToBounds = YES;
}

- (void)setInfoArray:(NSArray *)infoArray {
    
    _infoArray = infoArray;
    self.pageControl.numberOfPages = infoArray.count;
    [self.collectionView reloadData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.infoArray.count > 0 ? self.infoArray.count : 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MineInfoNewOneCellCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MineInfoNewOneCellCollectionViewCell" forIndexPath:indexPath];
    cell.backgroundColor = Color_Back_Gray;
    if (self.infoArray.count > 0) {
        cell.infoDic = self.infoArray[indexPath.row];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.delegate respondsToSelector:@selector(homeCell:withInfo:)]) {
        [self.delegate homeCell:self withInfo:self.infoArray];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    NSInteger currIndex = scrollView.contentOffset.x / CGRectGetWidth(scrollView.frame);
    self.pageControl.currentPage = currIndex;
}



@end
